﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Globalization;


namespace LPV_POS.mh_pos
{
    public partial class mh_dmban : Form
    {
        CultureInfo culture;
        string sMaKhuVuc = "";
        string sMaBan = "";

        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                col_OBJID.Caption = rm.GetString("mabanphong", culture);
                col_OBJNAME.Caption = rm.GetString("tenbanphong", culture);
                col_DM_LOAIDICHVU_NAME.Caption = rm.GetString("dichvu", culture);
                col_DM_BAN_LOAI_Name.Caption = rm.GetString("loaibanphong", culture);

                groupBox1.Text = rm.GetString("thongtinbanphong", culture);
                label3.Text = rm.GetString("loaidichvu", culture);
                label4.Text = rm.GetString("tenbanphong", culture);
                label5.Text = rm.GetString("soghe", culture);
                label6.Text = rm.GetString("ghichu", culture);
                lblBan_Loai.Text = rm.GetString("loaibanphong", culture);
                label8.Text = rm.GetString("tu", culture);
                label7.Text = rm.GetString("den", culture);
                btnThem.Text = rm.GetString("them", culture);
                btnSua.Text = rm.GetString("sua", culture);
                btnxoa.Text = rm.GetString("xoa", culture);
                bton_thoat.Text = rm.GetString("thoat", culture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        public mh_dmban(string _sMaKhuVuc)
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
            sMaKhuVuc = _sMaKhuVuc;
        }
        DataTable tbMain = new DataTable();
        private void mh_dmban_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            string str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);
            this.ShowComboBox();
            this.ShowData();
        }
        void ShowComboBox()
        {
            //grdBan
            string sSQL = "";

            thuvien othuvien = new thuvien();

            ///Ten_KHUVUC
            sSQL = "SELECT * FROM DM_KHUVUC WHERE OBJID LIKE '" + sMaKhuVuc + "'";
            DataTable tbKhuVuc = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            if (tbKhuVuc.Rows.Count > 0)
                lblTitle.Text = tbKhuVuc.Rows[0]["OBJNAME"] + "";

            sSQL = "SELECT * FROM DM_LOAIDICHVU ";//WHERE CO_TINH_TIENGIO <> 1";
            DataTable tbDM_LOAIDICHVU = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            if (tbDM_LOAIDICHVU.Columns.Contains("isRun"))
            {
                for (int i = 0; i < tbDM_LOAIDICHVU.Rows.Count; i++)
                    if (tbDM_LOAIDICHVU.Rows[i]["isRun"] + "" == "False")
                        tbDM_LOAIDICHVU.Rows.RemoveAt(i--);
            }
            cboLoaiDichVu.ValueMember = "OBJID";
            cboLoaiDichVu.DisplayMember = "OBJNAME";
            cboLoaiDichVu.DataSource = tbDM_LOAIDICHVU;
            //othuvien.f_showdataoncombox(cboLoaiDichVu, tbDM_LOAIDICHVU, "OBJID", "OBJNAME");
            //}

            ///cboDM_BAN_LOAI
            sSQL = "SELECT * FROM DM_BAN_LOAI dlS";
            DataTable tbDM_BAN_LOAI = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            cboLoaiBan.DisplayMember = "OBJNAME";
            cboLoaiBan.ValueMember = "OBJID";
            cboLoaiBan.DataSource = tbDM_BAN_LOAI;
            //othuvien.f_showdataoncombox(cboLoaiBan, tbDM_BAN_LOAI, "OBJID", "OBJNAME");
        }

        void ShowData()
        {
            //grdBan
            string sSQL = "SELECT DM_LOAIDICHVU_TEN = b.OBJNAME, a.*, " +
                          "     DM_BAN_LOAI_Name = dbl.OBJNAME " +
                          "FROM DM_BAN a " +
                          "     LEFT JOIN DM_LOAIDICHVU b ON a.DM_LOAIDICHVU_MA = b.OBJID " +
                          "     LEFT JOIN DM_BAN_LOAI dbl ON a.DM_BAN_LOAI_MA = dbl.OBJID " +
                          "WHERE a.MAKHUVUC LIKE '" + sMaKhuVuc + "' " +
                          "ORDER BY CAST(a.OBJID AS INT) ";

            thuvien othuvien = new thuvien();
            tbMain = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            grdBan.DataSource = tbMain;

        }
        void ShowInfo()
        {
            string sSQL = "SELECT * FROM DM_BAN WHERE OBJID LIKE '" + sMaBan + "'";
            DataTable tbDM_BAN = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            
            if (tbDM_BAN.Rows.Count > 0)
            {
                txtBan_Ten.Text = tbDM_BAN.Rows[0]["OBJNAME"] + "";

                cboLoaiDichVu.SelectedValue =("" + tbDM_BAN.Rows[0]["DM_LOAIDICHVU_MA"]);
                cboLoaiBan.SelectedValue = tbDM_BAN.Rows[0]["DM_BAN_LOAI_MA"];
    //
                decimal sSOGHE = 0;
                decimal.TryParse(tbDM_BAN.Rows[0]["SOGHE"] + "", out sSOGHE);
                txtsoghe.Value = sSOGHE;
                txtGhiChu.Text = tbDM_BAN.Rows[0]["GHICHU"] + "";
            }
        }
        public Boolean ShowForm()
        {
            this.ShowDialog();
            return this.DialogResult == DialogResult.OK;
        }
        Boolean iOK = false;
        private void bton_luu_Click(object sender, EventArgs e)
        {
            if (this.Save())
            {
                new mh_thongbao("Lưu thành công", 2).ShowDialog();                
                iOK = true;
                this.ShowData();

               // new mh_thongbao("Lưu thành công", 2).ShowDialog();
               // this.DialogResult = DialogResult.OK;
               //this.Close();

            }
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            //this.DialogResult = DialogResult.Cancel;
            if(iOK)
                this.DialogResult = DialogResult.OK;
            this.Dispose();
        }

        Boolean Save()
        {
            if (cboLoaiDichVu.SelectedIndex == -1)
            {
                new mh_thongbao("Chọn loại dịch vụ", 2).ShowDialog();
                return false;
            }
            else if (txtBan_Ten.Text.Trim() == "")
            {
                new mh_thongbao("Nhập tên bàn", 2).ShowDialog();
                return false;
            }

            if (cboLoaiBan.Visible)
            {
                if (cboLoaiBan.SelectedIndex == -1)
                {
                    LPV_POS.Module.MessageBox_LPV.Show(@"Chọn loại bàn\phòng");
                    return false;
                }
            }

            Boolean irt = false;
            object oDM_BAN_LOAI = null;
            if (cboLoaiBan.Visible)
                oDM_BAN_LOAI = cboLoaiBan.SelectedValue;
            if ((txtban_tu.Value == 0 && txtban_den.Value == 0) || (txtban_tu.Value >= txtban_den.Value))
            {
                sMaBan = new thuvien().SP_Update("sp_mh_dmban",
                                                new string[] { "@flag", "@OBJID", "@OBJNAME", "@MAKHUVUC", 
                                                            "@SOGHE", "@TRANGTHAI", "@DM_LOAIDICHVU_MA", "@GHICHU" ,
                                                            "@DM_BAN_LOAI_MA"},
                                                new object[] { 1,sMaBan,txtBan_Ten.Text.Trim(),sMaKhuVuc,
                                                            txtsoghe.Value,0,cboLoaiDichVu.SelectedValue,txtGhiChu.Text.Trim(),
                                                            oDM_BAN_LOAI}
                                                            ) + "";
                irt = true;
            }
            else
            {
                for (int i = (int)txtban_tu.Value; i <= (int)txtban_den.Value; i++)
                {

                    sMaBan = new thuvien().SP_Update("sp_mh_dmban",
                                                        new string[] { "@flag", "@OBJID", "@OBJNAME", "@MAKHUVUC", 
                                                            "@SOGHE", "@TRANGTHAI", "@DM_LOAIDICHVU_MA", "@GHICHU" ,
                                                            "@DM_BAN_LOAI_MA"},
                                                        new object[] { 1,-1,txtBan_Ten.Text+i,sMaKhuVuc,
                                                            txtsoghe.Value,0,cboLoaiDichVu.SelectedValue,txtGhiChu.Text.Trim(),
                                                            oDM_BAN_LOAI}
                                                            ) + "";

                    irt = true;
                }
            }
            return irt;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            sMaBan = "";
            txtBan_Ten.Text = "";
            txtsoghe.Value = 0;
            txtGhiChu.Text = "";

            btnLuu.Enabled = true;

            txtban_den.Enabled = true;
            txtban_tu.Enabled = true;
            btnban_den.Enabled = true;
            btnban_tu.Enabled = true;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            DataRow drban = gvBan.GetDataRow(gvBan.FocusedRowHandle);
            if (drban != null)
            {
                sMaBan = drban["OBJID"] + "";
                this.ShowInfo();
                btnLuu.Enabled = true;
                txtban_den.Enabled = false;
                txtban_tu.Enabled = false;
                btnban_den.Enabled = false;
                btnban_tu.Enabled = false;
            }
        }

        private void grdBan_Click(object sender, EventArgs e)
        {
            if (gvBan.RowCount == 1)
            {
                DataRow drban = gvBan.GetDataRow(gvBan.FocusedRowHandle);
                if (drban != null)
                    sMaBan = drban["OBJID"] + "";
                this.ShowInfo();    
            }
        }

        private void gvBan_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow drban = gvBan.GetDataRow(gvBan.FocusedRowHandle);
            if (drban != null)
                sMaBan = drban["OBJID"] + "";
            this.ShowInfo(); 
        }

        private void bton_tenkhuvuc_Click(object sender, EventArgs e)
        {
            mh_chu omh_chu = new mh_chu();
            string s = omh_chu.ShowForm(txtBan_Ten.Text, 200, 200, false);
            if (omh_chu.DialogResult == DialogResult.OK)
                txtBan_Ten.Text = s;
        }

        private void bton_ghichu_Click(object sender, EventArgs e)
        {
            mh_chu omh_chu = new mh_chu();
            string s = omh_chu.ShowForm(txtGhiChu.Text, 200, 200, false);
            if (omh_chu.DialogResult == DialogResult.OK)
                txtGhiChu.Text = s;
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {

            string sSQL = "DELETE FROM DM_BAN WHERE OBJID LIKE '" + sMaBan + "'";
            new thuvien().SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            new mh_thongbao("Xóa thành công", 2).ShowDialog();
            this.ShowData();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            string sss = new cpublic().loadformso_point(600, 200);             
            if (new cpublic().IsNumeric(Variable.str_so))
            {
                decimal so_luong = decimal.Parse(Variable.str_so);
                txtsoghe.Value = so_luong;
            }
        }

        private void btnban_tu_Click(object sender, EventArgs e)
        {
            string sss = new cpublic().loadformso_point(600, 200);
            if (new cpublic().IsNumeric(Variable.str_so))
            {
                decimal so_luong = decimal.Parse(Variable.str_so);
                txtban_tu.Value = so_luong;
            }
        }

        private void btnban_den_Click(object sender, EventArgs e)
        {
            string sss = new cpublic().loadformso_point(600, 200);
            if (new cpublic().IsNumeric(Variable.str_so))
            {
                decimal so_luong = decimal.Parse(Variable.str_so);
                txtban_den.Value = so_luong;
            }
        }

        private void cboLoaiDichVu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboLoaiDichVu.SelectedItem + "" != "")
            {
                DataRowView drv = (DataRowView)cboLoaiDichVu.SelectedItem;
                DataRow dr = drv.Row;
                if (dr != null)
                {
                    lblBan_Loai.Visible = dr["CO_TINH_TIENGIO"] + "" == "True";// cboLoaiDichVu.SelectedValue + "" == "2";
                    cboLoaiBan.Visible = dr["CO_TINH_TIENGIO"] + "" == "True"; //cboLoaiDichVu.SelectedValue + "" == "2";
                    col_DM_BAN_LOAI_Name.Visible = dr["CO_TINH_TIENGIO"] + "" == "True";// cboLoaiDichVu.SelectedValue + "" == "2";
                }
            }
        }
    }
}
